package com.screen.screendisplay.service.impl.wholeproduct;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.screen.screendisplay.entity.ne.NeProdoperPowerGs;
import com.screen.screendisplay.mapper.ne.NeProdoperPowerGsMapper;
import com.screen.screendisplay.service.wholeproduct.IWholeNePowerGenerationService;
import com.screen.screendisplay.vo.wholeproduct.NewEnergyPowerGenerationVO;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Calendar;

/**
 * <p>
 * 新能源发电情况 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-10-12
 */
@Service
public class WholeNePowerGenerationServiceImpl implements IWholeNePowerGenerationService {

    private final NeProdoperPowerGsMapper powerGsMapper;

    public WholeNePowerGenerationServiceImpl(NeProdoperPowerGsMapper powerGsMapper) {
        this.powerGsMapper = powerGsMapper;
    }

    @Override
    public NewEnergyPowerGenerationVO getNewEnergyPowerGeneration(Integer year) {
        NewEnergyPowerGenerationVO vo = new NewEnergyPowerGenerationVO();
        
        // 如果没有传入年份，使用当前年份
        if (year == null) {
            Calendar calendar = Calendar.getInstance();
            year = calendar.get(Calendar.YEAR);
        }
        
        // 1. 查询风电年度数据（power_type=0, stat_type=2）
        QueryWrapper<NeProdoperPowerGs> windQueryWrapper = new QueryWrapper<>();
        windQueryWrapper.eq("year", year)
                       .eq("power_type", 0)  // 0: 风电
                       .eq("stat_type", 2)   // 2: 年度统计
                       .orderByDesc("year")
                       .last("LIMIT 1");
        NeProdoperPowerGs windData = powerGsMapper.selectOne(windQueryWrapper);
        
        if (windData != null) {
            vo.setWindPlannedGeneration(windData.getPlannedGeneration());
            vo.setWindActualGeneration(windData.getActualGeneration());
            
            // 计算风电完成率 = 实际发电量 / 计划发电量 * 100
            if (windData.getPlannedGeneration() != null && 
                windData.getPlannedGeneration().compareTo(BigDecimal.ZERO) > 0) {
                BigDecimal windCompletionRate = windData.getActualGeneration()
                        .divide(windData.getPlannedGeneration(), 4, RoundingMode.HALF_UP)
                        .multiply(new BigDecimal("100"))
                        .setScale(2, RoundingMode.HALF_UP);
                vo.setWindCompletionRate(windCompletionRate);
            }
        } else {
            // 如果没有数据，设置为0
            vo.setWindPlannedGeneration(BigDecimal.ZERO);
            vo.setWindActualGeneration(BigDecimal.ZERO);
            vo.setWindCompletionRate(BigDecimal.ZERO);
        }
        
        // 2. 查询光伏年度数据（power_type=1, stat_type=2）
        QueryWrapper<NeProdoperPowerGs> pvQueryWrapper = new QueryWrapper<>();
        pvQueryWrapper.eq("year", year)
                     .eq("power_type", 1)  // 1: 光伏
                     .eq("stat_type", 2)   // 2: 年度统计
                     .orderByDesc("year")
                     .last("LIMIT 1");
        NeProdoperPowerGs pvData = powerGsMapper.selectOne(pvQueryWrapper);
        
        if (pvData != null) {
            vo.setPvPlannedGeneration(pvData.getPlannedGeneration());
            vo.setPvActualGeneration(pvData.getActualGeneration());
            
            // 计算光伏完成率 = 实际发电量 / 计划发电量 * 100
            if (pvData.getPlannedGeneration() != null && 
                pvData.getPlannedGeneration().compareTo(BigDecimal.ZERO) > 0) {
                BigDecimal pvCompletionRate = pvData.getActualGeneration()
                        .divide(pvData.getPlannedGeneration(), 4, RoundingMode.HALF_UP)
                        .multiply(new BigDecimal("100"))
                        .setScale(2, RoundingMode.HALF_UP);
                vo.setPvCompletionRate(pvCompletionRate);
            }
        } else {
            // 如果没有数据，设置为0
            vo.setPvPlannedGeneration(BigDecimal.ZERO);
            vo.setPvActualGeneration(BigDecimal.ZERO);
            vo.setPvCompletionRate(BigDecimal.ZERO);
        }
        
        return vo;
    }
}

